package org.jsola.hr.common;

import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.util.StyleUtil;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.jsola.hr.dto.excel.ExcelAxisDTO;

import java.util.List;

/**
 * 设置Excel标红,批注,和下拉的坐标
 *
 * @author lyh
 */
public class GenerateExcelHead implements CellWriteHandler {

    public GenerateExcelHead(List<ExcelAxisDTO> redAxisList, List<ExcelAxisDTO> annotationList, List<ExcelAxisDTO> dropDownList) {
        this.redAxisList = redAxisList;
        this.annotationList = annotationList;
        this.dropDownList = dropDownList;
    }

    /**
     * 标红坐标
     */
    List<ExcelAxisDTO> redAxisList;

    /**
     * 批注坐标
     */
    List<ExcelAxisDTO> annotationList;

    /**
     * 下拉坐标
     */
    List<ExcelAxisDTO> dropDownList;

    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {

    }

    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {

    }

    @Override
    public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {

    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                 List<CellData> cellDataList, Cell cell, Head head,
                                 Integer relativeRowIndex, Boolean isHead) {
        if (isHead) {
            int columnIndex = cell.getColumnIndex();
            Sheet sheet = writeSheetHolder.getSheet();
            Workbook workbook = sheet.getWorkbook();
            // 设置标红列
            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
            // 背景设置为蓝色
            headWriteCellStyle.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());
            // 字体
            WriteFont headWriteFont = new WriteFont();
            headWriteFont.setFontHeightInPoints((short) 11);
            headWriteFont.setBold(true);
            headWriteCellStyle.setWriteFont(headWriteFont);
            // 设置标红的字段
            if (CollectionUtils.isNotEmpty(redAxisList)) {
                for (ExcelAxisDTO redAxi : redAxisList) {
                    if (redAxi.getColumnIndex() == columnIndex && relativeRowIndex.equals(redAxi.getRowIndex())) {
                        // 设置字体颜色,这里也可以变成传参变为指定颜色
                        headWriteFont.setColor(IndexedColors.RED.index);
                    }
                }
            }
            headWriteCellStyle.setWriteFont(headWriteFont);
            CellStyle cellStyle = StyleUtil.buildHeadCellStyle(workbook, headWriteCellStyle);
            cell.setCellStyle(cellStyle);
            // 设置注释
            Drawing<?> drawing = sheet.createDrawingPatriarch();
            if (!CollectionUtils.isEmpty(annotationList)) {
                for (ExcelAxisDTO annotationAxis : annotationList) {
                    if (annotationAxis.getColumnIndex() == columnIndex &&
                            relativeRowIndex.equals(annotationAxis.getRowIndex())) {
                        // 批注内容
                        String context = annotationAxis.getAnnotation();
                        // 创建绘图对象,坐标主要是后四个参数
                        Comment comment = drawing.createCellComment(new XSSFClientAnchor(0, 0, 0, 0,
                                annotationAxis.getColumnIndex(), annotationAxis.getRowIndex(), columnIndex + 2, annotationAxis.getRowIndex() + 3));
                        comment.setString(new XSSFRichTextString(context));
                        cell.setCellComment(comment);
                    }
                }
            }
            // 设置下拉
            if (!CollectionUtils.isEmpty(dropDownList)) {
                for (ExcelAxisDTO dropDownAxis : dropDownList) {
                    if (dropDownAxis.getColumnIndex() == columnIndex &&
                            relativeRowIndex.equals(dropDownAxis.getRowIndex())) {
                        String[] dropDown = dropDownAxis.getDropDown();
                        DataValidationHelper dvHelper = sheet.getDataValidationHelper();
                        DataValidationConstraint dvConstraint = dvHelper.createExplicitListConstraint(dropDown);
                        CellRangeAddressList addressList;
                        DataValidation validation;
                        for (int i = dropDownAxis.getRowIndex() + 1; i < 1000; i++) {
                            // 设置下拉框的坐标
                            addressList = new CellRangeAddressList(i, i, columnIndex, columnIndex);
                            validation = dvHelper.createValidation(dvConstraint, addressList);
                            sheet.addValidationData(validation);
                        }
                    }
                }
            }
        }
    }
}
